package com.global.college.server;


import android.app.Service;
import android.content.Intent;
import android.os.Environment;
import android.os.IBinder;


import com.global.college.api.IDownloadListener;
import com.global.college.utils.LogUtils;
import com.global.college.utils.OkhttpUpdate;

import java.io.File;


/**
 * Created by Administrator on 2018/4/6.
 *  * @Description:专用下载APK文件Service工具类,通知栏显示进度,下载完成震动提示,并自动打开安装界面(配合xUtils快速开发框架) 需要添加权限：
 * <uses-permission android:name="android.permission.INTERNET" />
 * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 * <uses-permission android:name="android.permission.VIBRATE" />
 * <p>
 * 需要在<application></application>标签下注册服务
 */


public class DownAPKService extends Service {

    public static final String TAG = "DownAPKService";

    // 文件下载路径
    private String APK_url = "";
    // 文件保存路径(如果有SD卡就保存SD卡,如果没有SD卡就保存到手机包名下的路径)
    private String APK_dir = "";


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    public static DownApk lis;

    public interface DownApk {
        void onDownErrors();

        void onDownloading(int progress);

        void onDownFinsh();
    }

    public static void setDownApk(DownApk downlis) {
        lis = downlis;
    }

    //Service被创建时回调方法
    @Override
    public void onCreate() {
        super.onCreate();
        initAPKDir();// 创建保存路径

    }

    //Service启动时回调该方法
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // 接收Intent传来的参数:
        if (null != intent && intent.hasExtra("apk_url")) {
            APK_url = intent.getStringExtra("apk_url");
        }
        if (!APK_url.equals("")) {
            downLoadApk(APK_url);
        }

        return super.onStartCommand(intent, flags, startId);
    }

    private void initAPKDir() {
        /**
         * 创建路径的时候一定要用[/],不能使用[\],但是创建文件夹加文件的时候可以使用[\].
         * [/]符号是Linux系统路径分隔符,而[\]是windows系统路径分隔符 Android内核是Linux.
         */
        LogUtils.w("isHasSdcard:" + isHasSdcard());
        if (isHasSdcard())// 判断是否插入SD卡
        {
            APK_dir = Environment.getExternalStorageDirectory() + "/download/";// 保存到SD卡路径下
        } else {
            APK_dir = getApplicationContext().getFilesDir() + "/download/";// 保存到app的包名路径下
        }
        File destDir = new File(APK_dir);
        if (!destDir.exists()) {// 判断文件夹是否存在
            destDir.mkdirs();
        }
        LogUtils.w("APK_dir:" + APK_dir);
    }

    /**
     * @Description:判断是否插入SD卡
     */
    private boolean isHasSdcard() {
        String status = Environment.getExternalStorageState();
        if (status.equals(Environment.MEDIA_MOUNTED)) {
            return true;
        } else {
            return false;
        }
    }

    public void downLoadApk(String url) {
        OkhttpUpdate.getInstance().download(url, "download", new IDownloadListener() {
            @Override
            public void onDownloadSuccess() {
                lis.onDownFinsh();
                stopSelf();
            }

            @Override
            public void onDownloading(int progress) {
                lis.onDownloading(progress);
            }

            @Override
            public void onDownloadFailed() {
                lis.onDownErrors();
                stopSelf();
            }
        });

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        stopSelf();
    }

}
